﻿#include "setup5wind.h"
#include "qevent.h"
#include "ui_setup5wind.h"
#include <mainwindow.h>
#include "speechmodulethread.h"
extern intListener doorStatus;
QString comtest;
int speflv;

setup5wind::setup5wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup5wind)
{
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    ui->setupUi(this);
    ui->radioButton->setChecked(true);
    speechmd = new speechModuleThread();


    QIntValidator *validator = new QIntValidator(this);
    validator->setRange(0, 65535); // 设置接受的数字范围
    QRegExp regExp("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    QRegExpValidator *validator2 = new QRegExpValidator(regExp, this);
    ui->ip_le_2->setValidator(validator2);
    ui->port_le_2->setValidator(validator);
    ui->tabWidget->setStyleSheet("QTabWidget:pane {border-top:0px solid #e8f3f9;background:  transparent; }"
                                 "QTabBar::tab { background-color: transparent; }");

    ui->comboBox->blockSignals(true);
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {
        ui->comboBox->addItem(info.portName());
    }
    ui->comboBox->setCurrentText("ttyS4");
    ui->comboBox->blockSignals(false);
    connect(&doorStatus, &intListener::valueChanged,this,&setup5wind::doorStatusChanged,Qt::QueuedConnection);
    QWidget *pageToHide = ui->tabWidget->widget(1);
    // 移除页面（但不删除它）
    ui->tabWidget->removeTab(1);
    ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_9->setEnabled(false);
}
void setup5wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
setup5wind::~setup5wind()
{
    delete ui;
}
void setup5wind::showEvent(QShowEvent *event)
{
    tcpClient=TcpClient::getInstance();
    ui->ck_cb1_2->clear();
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {
        ui->ck_cb1_2->addItem(info.portName());
    }
    QString sql="Select * from ConnectionInfo";

    QSqlDatabase db = dbHelper::openConnection();

    QSqlQuery query(db);
    query.exec(sql);
    while (query.next()) {
        ui->ip_le_2->setText(query.value("Ip").toString());
        ui->port_le_2->setText(query.value("Port").toString());
    }
    query.clear();

    if(tcpClient->IsOnline()){
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
        ui->label_6->setText("已连接");
    }else{
        ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
        ui->label_6->setText("未连接");
    }


    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    event->accept();

}

void setup5wind::on_SaveBtn_clicked()
{
    if(ui->tabWidget->currentIndex()==0){
        if(MainWindow::connectType==0){
            if(ui->ip_le_2->text()!=nullptr && ui->port_le_2->text()!=nullptr){
                QString ip = ui->ip_le_2->text();
                quint16 port = ui->port_le_2->text().toUInt();

                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString insertSql= QString("update ConnectionInfo set IP='%1',Port='%2';").arg(ip,QString::number(port));
                query.exec(insertSql);

                tcpClient= TcpClient::getInstance();
                tcpClient->ClientConnectToHost(ip, port);

                thread()->sleep(1);
                if(MainWindow::tcpStatus.get()==1){
                    tcts *tctsWindow = new tcts();
                    tctsWindow->infochange("提示","error","连接失败",false,true);
                    tctsWindow->show();

                    ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx1.png);");
                    ui->label_6->setText("未连接");
                }else {
                    tcts *tctsWindow = new tcts();
                    tctsWindow->infochange("提示","treat","连接成功",false,true);
                    tctsWindow->show();
                    ui->label_5->setStyleSheet("border-image: url(:/pic/picture/tx2.png);");
                    ui->label_6->setText("已连接");
                }
            }
        }
        else if(MainWindow::connectType==1)
        {
            QString serialPort = ui->ck_cb1_2->currentText();
            QString baudRate = ui->ck_cb2_2->currentText();

            QSqlDatabase db = dbHelper::openConnection();
            QSqlQuery query(db);

            QString insertSql= QString("update ConnectionInfo set SerialPort='%1',BaudRate='%2';").arg(serialPort,baudRate);
            query.exec(insertSql);
        }
    }else if(ui->tabWidget->currentIndex()==1){
        speflv = 1;
        comtest = ui->comboBox->currentText();
        qDebug()<<comtest;
        MainWindow::speechflagkg.set(0);
    }
}

void setup5wind::on_SaveBtn_2_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup5wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup5wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
        doorStatus.set(1);
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
        doorStatus.set(0);
    }
}


void setup5wind::on_comboBox_currentTextChanged(const QString &arg1)
{

}

void setup5wind::on_radioButton_clicked(bool checked)
{
    if(checked){
        ui->stackedWidget->setCurrentIndex(0);
        MainWindow::connectType=0;
    }
}

void setup5wind::on_radioButton_2_clicked(bool checked)
{
    if(checked){
        ui->stackedWidget->setCurrentIndex(1);
        MainWindow::connectType=1;
    }
}

